package com.wending.base_method;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * Created by 张英杰 on 2015/10/29.
 * 检查网络
 */
public class NetworkDetector {
    public static boolean detect(Activity act) {
        ConnectivityManager manager = (ConnectivityManager) act
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }

    public static boolean detect(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo networkinfo = manager.getActiveNetworkInfo();
        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }

    /**
     * 获取wifiid信息
     *
     * @param context
     * @return
     */
    public static String getWifiInfo(Context context) {
        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiinfo = wifi.getConnectionInfo();
        return wifiinfo != null ? wifiinfo.getSSID() : null;
    }

    /**
     * 检查wifi是否可用
     *
     * @param context
     * @return
     */
    public static boolean checkWifiState(Context context) {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int wifiState = manager.getWifiState();
        if (wifiState == manager.WIFI_STATE_ENABLED || wifiState == manager.WIFI_STATE_ENABLING) {
            return true;
        } else {
            return false;
        }
    }
}
